package il.ac.braude.sona.entities;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
/**
 * This class is used to transfer files
 * @author  Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 * Laboratory in Software Engineering,Ort Braude College.</p>
 *
 */
public class FileTransfer implements Serializable {
	private String name;
	private long rid;
	private byte[] fileBytes;
	public FileTransfer(File f) throws IOException {
		FileInputStream fr = new FileInputStream(f);
		name=f.getName();
		if (f.length() > Integer.MAX_VALUE) {
			fr.close();
			throw new IOException("file is too big");
		}
		fileBytes=new byte[(int) f.length()];
		fr.read(fileBytes);
		fr.close();
	}
	/**
	 * f shouldnt exist or it will be rewritten
	 * @param f
	 * @throws IOException 
	 */
	public void writeToFile(File f) throws IOException {
		if (!f.exists()) {
			f.getParentFile().mkdirs();
			f.createNewFile();
		}
		FileOutputStream fos = new FileOutputStream(f);
		fos.write(fileBytes);
		fos.close();
	}
	/**
	 * 
	 * @return length of the file
	 */
	public long length() {
		return fileBytes.length;
	}
	@Override
	public String toString() {
		return name;
	}
	/**
	 * 
	 * @param id change request id
	 */
	public void setChangeRequestId(long id) {
		rid=id;
	}
	/**
	 * 
	 * @return change request id
	 */
	public long getChangeRequestId() {
		return rid;
	}
}
